
<?php
$form = $this->beginWidget('CActiveForm', array(
    "id" => "saveManageEventForm",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
$url = $control->getAjaxLinkUrl('saveManageEventContent', NULL, array(
    'eventId' => $eventId, 'manageView' => $manageView, 'contentId' => $contentId));
$updateId = $control->getAjaxContainerId();
?>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function addManageEvent<?php echo $eventId; ?>() {
        var teacherName = $('#BDynamicFormModel_media_teacherName_event_info').val();
        var height = $('#BDynamicFormModel_media_height_event_info').val();
        var weight = $('#BDynamicFormModel_media_weight_event_info').val();
        var eventInfo = $('#BDynamicFormModel_media_info_event_info').val();
        var filepath = $('#BDynamicFormModel_image_media_image_event_info').val();
        var extStart = filepath.lastIndexOf(".");
        var ext = filepath.substring(extStart, filepath.length).toUpperCase();
        if (teacherName != "") {
            if (height != "") {
                if (weight != "") {
                    if (eventInfo != "") {
                        if (filepath != "") {
                            if (ext == ".BMP" || ext == ".PNG" || ext == ".GIF" || ext == ".JPG" || ext == ".JPEG") {
                                var formData = new FormData($("#saveManageEventForm")[0]);
                                $.ajax({
                                    url: '<?php echo $url; ?>',
                                    type: 'POST',
                                    data: formData,
                                    //datatype: 'json',
                                    // async: false,
                                    beforeSend: function() {
                                        $("#ajax-loaderdiv").show();
                                    },
                                    success: function(html) {
                                        // on success do some validation or refresh the content div to display the uploaded images 
                                        jQuery("#<?php echo $updateId; ?>").html(html);
                                    },
                                    complete: function() {
                                        $("#ajax-loaderdiv").hide();
                                    },
                                    error: function(data) {
                                        alert("There may a error on uploading. Try again later");
                                    },
                                    cache: false,
                                    contentType: false,
                                    processData: false
                                });
                            } else
                                alert("图片限于bmp,png,gif,jpeg,jpg格式");
                        } else
                            alert("请上传图片")
                    } else
                        alert("请输入个人简介");
                } else
                    alert("请输入体重");
            } else
                alert("请输入身高");
        } else
            alert("请输入姓名");

        return false;
    }
</script>
<legend>
    <h4>
        <?php echo $control->getAjaxLink(array('name' => '活动列表', 'id' => 'showManage')); ?>\
        <?php echo $control->getAjaxLink(array('name' => '管理活动', 'id' => 'manageEvent'), array('eventId' => $eventId, 'manageView' => $manageView)); ?>\
        <?php echo '管理活动内容'; ?>
    </h4>
</legend>

<?php
if (!isset($model))
    $model = new BDynamicFormModel ();

//$eventNameField = 'media_name_event_info';
//$eventDescriptionField = 'media_description_event_info';
$eventImageField = 'image_media_image_event_info';

//$eventTypeField = 'media_eventTypeId_event_info';
$eventTeacherNameField = 'media_teacherName_event_info';
$eventHeightField = 'media_height_event_info';
$eventWeightField = 'media_weight_event_info';
$eventInfoField = 'media_info_event_info';
$model->addField($eventTeacherNameField);
$model->addField($eventHeightField);
$model->addField($eventWeightField);
$model->addField($eventInfoField);
$model->addField($eventImageField);
$eventTable = new BAEventSheet();
$event = $eventTable->getListById($eventId);


if ($contentId > 0) {
    $manageEventContentTable = new BAEventSheet($event);
    $eventContentView = $manageEventContentTable->getView();
    $eventContent = $manageEventContentTable->getListById($contentId);
    $teachernameItem = $eventContent->getItem('teacherName');
    $imageItem = $eventContent->getItem('image');
    $heightItem = $eventContent->getItem('height');
    $weightItem = $eventContent->getItem('weight');
    $infoItem = $eventContent->getItem('info');

    if ($teachernameItem instanceof BTreeItem) {
        $teachername = $teachernameItem->getValue();
        $model->$eventTeacherNameField = $teachername;
    }

    if ($imageItem instanceof BTreeItem) {
        $image = $imageItem->getValue();
        $model->$eventImageField = $image;
    }
    if ($heightItem instanceof BTreeItem) {
        $height = $heightItem->getValue();
        $model->$eventHeightField = $height;
    }
    if ($weightItem instanceof BTreeItem) {
        $weight = $weightItem->getValue();
        $model->$eventWeightField = $weight;
    }
    if ($infoItem instanceof BTreeItem) {
        $info = $infoItem->getValue();
        $model->$eventInfoField = $info;
    }
}
$eventName = $event->getItem('name')->getValue();
?>
<?php
$app = Blyn::app()->getActiveApp();
?>

<table class="table table-hover">
    <colgroup>
        <col class="span2">
        <col class="span4">
        <col class="span4">
    </colgroup>
    <thead>
        <tr>
            <th><?php echo $eventName ?></th>
            <th></th>
            <th><?php echo '说明'; ?></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
<?php echo '姓名'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $eventTeacherNameField, array('placeholder' => 'teachername', 'required' => true));
                ?>
                <!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
            </td>
            <td></td>
        </tr>
        <tr><td> <?php echo '身高'; ?> </td>
            <td>
<?php
echo CHtml::activeTextField($model, $eventHeightField, array('placeholder' => 'height' . '...', 'required' => true));
?>
            </td>
            <td></td>
        <tr><td> <?php echo '体重'; ?> </td>
            <td>
<?php
echo CHtml::activeTextField($model, $eventWeightField, array('placeholder' => 'weight' . '...', 'required' => true));
?>
            </td>
            <td></td>
        </tr> 
        <tr><td> <?php echo '个人简介'; ?> </td>
            <td>
<?php
echo CHtml::activeTextArea($model, $eventInfoField, array('placeholder' => 'info' . '...', 'required' => true));
?>
            </td>
            <td></td>
        </tr>
        </tr>  
        <tr><td> <?php echo '个人头像' ?> </td>
            <td>
<?php
echo CHtml::activeFileField($model, $eventImageField, array('placeholder' => 'image' . '...', 'required' => true));
if ($model->$eventImageField != NULL) {
    $imageUrl = Yii::app()->request->baseUrl . $model->$eventImageField;
    echo "<img src=" . $imageUrl . " width=50 height=50 />";
}
?>
            </td>
            <td></td>
        </tr>   

    </tbody>
</table>
<?php
$this->endWidget();
//echo CHtml::endForm();
?>
<button class="btn" onclick="javascript:addManageEvent<?php echo $eventId; ?>()"><?php echo '保存' ?></button>
